<?php
namespace Modules\ShopApi\Models; 
use Modules\Admin\Services\UserService;
use Illuminate\Support\Facades\DB;
use Modules\ShopApi\Models\ShopCart;
use Modules\ShopApi\Models\ShopProduct;
class ShopOrder extends BaseApiModel
{	
	/**
     * @name  商品详情立即购买 
     */
	public function add_product_order($data){
		$user_info = (new UserService())->my(); 
		$site_info = DB::table('shop_sites')->where(['user_id'=>$user_info['id'],'status'=>1])->select('name','phone','province_id','city_id','county_id','detailed_address')->first();
		if(!$site_info){
			return $this->api_error(['msg'=>'请选择收货地址！']);
		}	
		$order_data = $this->add_product_order_data($site_info,$user_info,$data);
		$id = $this->insertGetId($order_data);
		if($id){
			return $this->api_success(['data'=>[ 
				'id'=>$id
			]]);
		}else{
			return $this->api_error(['msg'=>'购买失败，请重试！']);
		}
	}
	/**
     * @name  商品详情立即购买数据处理 
     */
	private function add_product_order_data($site_info,$user_info,$data){
		$specs_info = DB::table('shop_product_specs')->where(['id'=>$data['specs_id']])->select('money','postage')->first();
		$province_name = DB::table('areas')->where('id',$site_info->province_id)->value('name');
		$city_name = DB::table('areas')->where('id',$site_info->city_id)->value('name');
		$county_name = DB::table('areas')->where('id',$site_info->county_id)->value('name');
		$content = [[
			'id'=>$data['product_id'],//商品id
			'name'=>DB::table('shop_products')->where('id',$data['product_id'])->value('name'),//商品名称
			'specs_id'=>$data['specs_id'],//规格id
			'specs'=>$data['specs'],//规格详情
			'num'=>$data['num'],//数量
			'money'=>$specs_info->money,//商品价格
			'postage'=>$specs_info->postage,//商品邮费
		]];
		$order_data = [
			'user_id'=>$user_info['id'],
			'order_no'=>make_order_no(),
			'total_money'=>($specs_info->money * $data['num']) + ($specs_info->postage * $data['num']),
			'deliver_money'=>$specs_info->postage * $data['num'],
			'created_at'=>date('Y-m-d H:i:s'),
			'total_num'=>$data['num'],
			'user_address'=>$province_name . $city_name . $county_name . $site_info->detailed_address,
			'user_name'=>$site_info->name,
			'user_phone'=>$site_info->phone,
			'province_id'=>$site_info->province_id,
			'city_id'=>$site_info->city_id,
			'county_id'=>$site_info->county_id,
			'content'=>s_json_encodes($content)
		];
		return $order_data;
	}
	/**
     * @name  购物车结算 
     */
	public function add_cart_order($ids){
		$user_info = (new UserService())->my(); 
		$site_info = DB::table('shop_sites')->where(['user_id'=>$user_info['id'],'status'=>1])->select('name','phone','province_id','city_id','county_id','detailed_address')->first();
		if(!$site_info){
			return $this->api_error(['msg'=>'请选择收货地址！']);
		}	
		$ShopCart = new ShopCart();
		$specs_info = $ShopCart->ids_cart_list($ids);
		$order_data = $this->add_cart_order_data($site_info,$user_info,$specs_info);
		DB::beginTransaction();
		try{
			$id = $this->insertGetId($order_data);
			$ShopCart->destroy($ids);
		DB::commit();
		}catch(\Exception $e){
			DB::rollBack();
			return $this->api_error();
		}
		return $this->api_success(['data'=>[ 
				'id'=>$id
			]]);
	}
	/**
     * @name 购物车结算数据处理 
     */
	private function add_cart_order_data($site_info,$user_info,$specs_info){
		$province_name = DB::table('areas')->where('id',$site_info->province_id)->value('name');
		$city_name = DB::table('areas')->where('id',$site_info->city_id)->value('name');
		$county_name = DB::table('areas')->where('id',$site_info->county_id)->value('name');
		$content = [];
		$total_money = 0;
		$postage = 0;
		$num = 0;
		foreach($specs_info as $k=>$v){
			$content[] = [
					'id'=>$v['product_id'], 
					'name'=>$v['product_to']['name'],
					'specs_id'=>$v['specs_id'],
					'num'=>$v['num'],
					'money'=>$v['specs_to']['money'],
					'postage'=>$v['specs_to']['postage'],
			];
			$total_money = $total_money + ($v['specs_to']['money'] * $v['num']) + ($v['specs_to']['postage'] * $v['num']);
			$postage = $postage + ($v['specs_to']['postage'] * $v['num']);
			$num += $v['num'];
		}
		$order_data = [
			'user_id'=>$user_info['id'],
			'order_no'=>make_order_no(),
			'total_money' => $total_money,
			'deliver_money'=>$postage,
			'created_at'=>date('Y-m-d H:i:s'),
			'total_num'=>$num,
			'user_address'=>$province_name . $city_name . $county_name . $site_info->detailed_address,
			'user_name'=>$site_info->name,
			'user_phone'=>$site_info->phone,
			'province_id'=>$site_info->province_id,
			'city_id'=>$site_info->city_id,
			'county_id'=>$site_info->county_id,
			'content'=>s_json_encodes($content)
		];
		return $order_data;
	}
	/**
     * @name  订单列表
     */
	public function order_list($data){
		$user_info = (new UserService())->my(); 
		$model = $this->query();
		$model = $model->where('user_id',$user_info['id']);
		if($data['type'] > 0){
			if($data['type'] < 5){
				$model = $model->where('status',$data['type']);
			}else{
				$model = $model->whereIn('status',[5,6,7,8]);
			}
			
		}
		$list = $model->select('id','order_no','status','total_money','deliver_money','total_num','content')
				->orderBy('id','desc')
				->paginate(config('shopapi.per_page_product'))
				->toArray();	
		$list['data'] = $this->order_image_list($list['data']);
		return $this->api_success(['data'=>[
			'data'			=> $list['data'],
			'last_page'		=> $list['last_page'],
			'per_page'		=> $list['per_page'],
			'current_page'	=> $list['current_page'],
			'total' 		=> $list['total']
		]]);			
	}
	/**
     * @name  订单列表数据处理
     */
	private function order_image_list($data){
		$ShopProduct = new ShopProduct();
		$http = getHttpType();	 
		foreach($data as $k=>$v){
			$v['content'] = json_decode($v['content'],true);
			foreach($v['content'] as $k1=>$v1){
				$info = $ShopProduct->select('id','image_id')
									->with([
									'image_one'=>function($query){
										$query->select('id','url','open');
									}
									])
									->where(['id'=>$v1['id']])
									->first()
									->toArray();
				if($info['image_one']['open'] == 1){
					$v['content'][$k1]['image_url'] = $http . $info['image_one']['url'];
				}else{
					$v['content'][$k1]['image_url'] = $info['image_one']['url'];
				}					
			}
			$data[$k]['content'] = $v['content'];
		}
		return $data;	
	}
}
